<?php
/**
 * @link https://ym2.cleverstone.top
 * @copyright Copyright (c) 2020 Yii Manager Software LLC
 */

namespace backend\controllers;

use Yii;
use builder\base\BaseController;
use builder\form\FormBuilder;

/**
 * 系统设置
 * @author cleverstone
 * @since ym2.0
 */
class SettingController extends BaseController
{
    public $actionVerbs = [
        'view' => ['GET', 'POST'],
    ];

    /**
     * 设置选项卡
     * @return string
     * @throws \Throwable
     */
    public function actionView()
    {
        // 视图构建器
        $viewBuilder = $this->viewBuilder;
        if ($this->isPost) {
            switch ($viewBuilder->builderId) {
                case 'setting_system': // 系统
                    return $this->system();
                default:
                    return $this->asFail('无逻辑分配, 配置项保存失败');
            }
        } else {
            // 选项卡
            $viewBuilder->tab
                ->setId('setting_view')
                ->setTheme('column')
                ->setTitle('设置')
                ->setTabItem([
                    '系统' => $this->tabItem->builder($this->system())->switchReload()->selected(),
                ]);

            return $viewBuilder->render();
        }
    }

    /**
     * 系统
     * @return FormBuilder|string
     * @throws \Throwable
     */
    protected function system()
    {
        $sysSetting = Yii::$app->setting;
        if ($this->isPost) {
            $bodyParams = $this->post;

            $sysSetting->write('SYS.SYS_TLOG', $bodyParams['SYS_TLOG'], '系统操作日志开关')
                ->write('SYS.SYS_SSO', $bodyParams['SYS_SSO'], '系统单点登录开关')
                ->write('SYS.SYS_PROTECT', $bodyParams['SYS_PROTECT'], '系统维护开关')
                ->write('SYS.SYS_PROTECT_TIPS', $bodyParams['SYS_PROTECT_TIPS'], '系统维护提示语')
                ->invalidateCache();

            return $this->asOk('【系统】配置项更新成功');
        } else {
            $form = FormBuilder::instance(['id' => 'setting_system']);

            return $form->setSubmitUrl(['setting/view', $form->idParam => $form->id])
                ->setRootLayout(['class' => 'col-md-6 px-0'])
                ->setFooterLayout(['class' => 'text-left'])
                ->setFormControl([
                    'SYS_TLOG' => $this->formControl->sw()->checked($sysSetting->read('SYS.SYS_TLOG', 1))->label('系统操作日志开关'),
                    'SYS_SSO' => $this->formControl->sw()->checked($sysSetting->read('SYS.SYS_SSO'))->label('系统单点登录开关'),
                    'SYS_PROTECT' => $this->formControl->sw()->checked($sysSetting->read('SYS.SYS_PROTECT'))->label('系统维护开关'),
                    'trigger' => $this->formControl->trigger()->when('default', 'SYS_PROTECT', '==', 1)->triggerItems([
                        'SYS_PROTECT_TIPS' => $this->formControl->textarea()->defaultValue($sysSetting->read('SYS.SYS_PROTECT_TIPS'))->label('系统维护提示语'),
                    ])
                        ->uiClass(['col-md-12', 'p-0'])
                        ->display(),
                ])
                ->setSubmitConfirm()
                ->setResetBtn()
                ->setSubmitBtn('确认配置');
        }
    }
}